<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const arr = Array(4).fill().map((item) => {
            return item = Array(4).fill(".")
        })
        console.log(arr);
        
        
        console.log("------------");
        
        function solveNQueens(n) {
            let solutions = [];
            let board = Array(n).fill().map((item) => {
                return item = Array(n).fill(".")
            });
            function isSafe(row, col) {
                // 检查列是否有皇后
                for (let i = 0; i < row; i++) {
                    if (board[i][col] === 'Q') {
                        return false;
                    }
                }

                // 检查左上对角线
                for (let i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
                    if (board[i][j] === 'Q') {
                        return false;
                    }
                }

                // 检查右上对角线
                for (let i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
                    if (board[i][j] === 'Q') {
                        return false;
                    }
                }

                return true;
            }

            function solve(row) {
                if (row === n) {
                    // 如果成功放置了所有皇后，则记录解
                    solutions.push(board.map(row => row.join('')));
                    return;
                }

                for (let col = 0; col < n; col++) {
                    if (isSafe(row, col)) {
                        board[row][col] = 'Q';
                        solve(row + 1);
                        board[row][col] = '.'; // 回溯，撤销选择
                    }
                }
            }

            solve(0);
            return solutions;
        }

        // 使用示例
        let n = 4;
        let result = solveNQueens(4);
        console.log(result);
        
    </script>
</body>

</html>